package utils;

import FCRendering.FCRMaster;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import utils.entity.UIConfigEntity;

import java.awt.*;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * 解析excel文件里的坐标判断点击
 */
public class FCUIConfig {
    private static String TAG_KEY_EN = "英文";
    private static String TAG_KEY_CN = "中文";
    private static String TAG_LEFT_TOP = "左上角坐标";
    private static String TAG_RIGHT_BOTTOM = "右下角坐标";
    private static String TAG_SELECT_POINT = "绘图区域左上坐标";
    private static String TAG_ENTER_BU = "EnterBu";

    private static FCUIConfig config;

    private HashMap<Integer, UIConfigEntity> selectBuMaps = new HashMap<>();
    private HashMap<String, UIConfigEntity> uiConfigMaps = new HashMap<>();
    private UIConfigEntity defEntity = new UIConfigEntity();//默认的空的

    static {
        config = new FCUIConfig();
    }

    public static FCUIConfig getIn() {
        return config;
    }

    public FCUIConfig() {
        defEntity = new UIConfigEntity();
        defEntity.setUiRect(new Rectangle());
    }

    public UIConfigEntity getByKey(String key_en) {
        UIConfigEntity entity = uiConfigMaps.get(key_en);
        if (entity == null) {
            return defEntity;
        }
        return entity;
    }

    public int getEnterBuByKey(String key_en) {
        UIConfigEntity entity = uiConfigMaps.get(key_en);
        if (entity != null) {
            return entity.getEnterBuKey();
        }
        return 0;
    }
    public UIConfigEntity getEntityByEnterBu(int bu) {
       return selectBuMaps.get(bu);
    }

    //重设分辨率
    public void resetProportion(double p) {
        System.out.println("获取得分辨率比例是：" + p);
        Iterator<Map.Entry<String, UIConfigEntity>> iterator = uiConfigMaps.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, UIConfigEntity> data = iterator.next();
            UIConfigEntity configEntity = data.getValue();
            int x = configEntity.getLoadRect().x;
            int y = configEntity.getLoadRect().y;
            int w = configEntity.getLoadRect().width;
            int h = configEntity.getLoadRect().height;
            configEntity.setSelectBuX((int)(configEntity.getLoadSelectBuX()* FCRMaster.fcv.getProportion()));
            configEntity.setSelectBuY((int)(configEntity.getLoadSelectBuY()* FCRMaster.fcv.getProportion()));
            configEntity.setUiRect(new Rectangle((int) (x * FCRMaster.fcv.getProportion()), (int) (y * FCRMaster.fcv.getProportion()),
                    (int) (w * FCRMaster.fcv.getProportion()), (int) (h * FCRMaster.fcv.getProportion())));
            if(configEntity.getKey_en().equals("singlePlayBack")){
                System.out.println(configEntity.getUiRect().toString());
            }
        }
    }

    public void parseConfigFile() {
        try {

            File file = new File("./material/config/界面按键坐标.xls");
            // 创建输入流，读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
            // jxl提供的Workbook类
            Workbook wb = Workbook.getWorkbook(is);

            Sheet sheet = wb.getSheet(0);
            int rowkeyEn = 0, rowKeycn = 0, rowlefttop = 0, rowrightbottom = 0, rowSelectBu = 0, rowEnterBu = 0;
            // sheet.getRows()返回该页的总行数
            for (int i = 0; i < sheet.getRows(); i++) {
                if (i == 0) {
                    for (int j = 0; j < sheet.getColumns(); j++) {
                        Cell cell_1 = sheet.getCell(j, i);
                        if (cell_1 == null || cell_1.getContents().isEmpty()) {
                            continue;
                        }
                        String content = cell_1.getContents().trim();
                        if (content.contains(TAG_KEY_EN)) {
                            rowkeyEn = j;
                        } else if (content.contains(TAG_KEY_CN)) {
                            rowKeycn = j;
                        } else if (content.contains(TAG_LEFT_TOP)) {
                            rowlefttop = j;
                        } else if (content.contains(TAG_RIGHT_BOTTOM)) {
                            rowrightbottom = j;
                        } else if (content.contains(TAG_SELECT_POINT)) {
                            rowSelectBu = j;
                        } else if (content.contains(TAG_ENTER_BU)) {
                            rowEnterBu = j;
                        }
                    }
                } else {
                    String content = getCell(sheet, rowkeyEn, i);
                    if (content == null) {
                        continue;
                    }
                    UIConfigEntity configEntity = new UIConfigEntity();
                    int x = 0, y = 0, x2 = 0, y2 = 0;
                    configEntity.setKey_en(content);

                    content = getCell(sheet, rowKeycn, i);
                    configEntity.setKey_cn(content);

                    content = getCell(sheet, rowEnterBu, i);
                    if (content != null) {
                        configEntity.setEnterBuKey(Integer.parseInt(content));
                        selectBuMaps.put(configEntity.getEnterBuKey(), configEntity);
                    }
                    content = getCell(sheet, rowSelectBu, i);
                    if (content != null) {
                        content = content.replace("，", ",");
                        String[] split = content.split(",");
                        if (split.length > 1) {
                            configEntity.setLoadSelectBuX(Integer.parseInt(split[0]));
                            configEntity.setLoadSelectBuY(Integer.parseInt(split[1]));
                        }
                    }

                    content = getCell(sheet, rowlefttop, i);
                    content = content.replace("，", ",");
                    String[] split = content.split(",");
                    if (split.length > 1) {
                        x = Integer.parseInt(split[0]);
                        y = Integer.parseInt(split[1]);
                    }
                    content = getCell(sheet, rowrightbottom, i);
                    content = content.replace("，", ",");
                    split = content.split(",");
                    if (split.length > 1) {
                        x2 = Integer.parseInt(split[0]);
                        y2 = Integer.parseInt(split[1]);
                    }
                    //坐标因为有一个加载的分辨率比例所以要还原回1920的
                    double p = FCExGlobalConfigUtil.getIn().getLoadProportion();
                    configEntity.setLoadSelectBuX((int)(configEntity.getLoadSelectBuX()/p));
                    configEntity.setLoadSelectBuY((int)(configEntity.getLoadSelectBuY()/p));
                    configEntity.setLoadRect(new Rectangle((int) (x / p), (int) (y / p), (int) ((x2 - x) / p), (int) ((y2 - y) / p)));
                    uiConfigMaps.put(configEntity.getKey_en(), configEntity);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getCell(Sheet sheet, int index, int i) {
        if (index < 0) {
            System.out.println("getCell index < 0");
            return null;
        }
        Cell cell = sheet.getCell(index, i);
        if (cell == null || cell.getContents() == null || cell.getContents().isEmpty()) {
            return null;
        }
        return cell.getContents().trim();
    }

    public static String getCellDef(Sheet sheet, int index, int i, String def) {
        String d = getCell(sheet, index, i);
        if(d != null){
            return d;
        }
        return def;
    }

    public static int getCellInt(Sheet sheet, int index, int i,int def) {
       String d = getCell(sheet, index, i);
       if(d != null){
            return Integer.parseInt(d);
       }
       return def;
    }

    public static double getCellDouble(Sheet sheet, int index, int i,double def) {
        String d = getCell(sheet, index, i);
        if(d != null){
            return Double.parseDouble(d);
        }
        return def;
    }

    public boolean checkUIClick(String key_en, int x, int y) {
        UIConfigEntity entity = getByKey(key_en);
        if (entity.getUiRect().contains(x, y)) {
            return true;
        }
        return false;
    }



}
